package com.lins.storedisk.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.lins.disk.bo.UserBo;
import com.lins.disk.dto.LoginDTO;
import com.lins.disk.dto.RegisterDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户相关接口
 * @Author linshuo
 * @Date 2023/12/1 10:44
 */
@Slf4j
//@SaIgnore
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserBo userBo;

    @PostMapping({"register"})
    public SaResult regUser(@Validated RegisterDTO dto) {
        return SaResult.ok(userBo.regUser(dto)>0?"注册成功":"未发生异常，但注册失败");
    }

    @RequestMapping("login")
    public Map<String,String> login(LoginDTO dto, HttpServletRequest request) {
        dto.setRequest(request);
        return userBo.doLogin(dto);
    }

    @RequestMapping("doLogin")
    public SaResult doLogin(LoginDTO dto, HttpServletRequest request) {
        dto.setRequest(request);
        return SaResult.data(userBo.doLogin(dto));
    }

    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @RequestMapping("doLogout")
    public SaResult doLogout() {
        //先检查当前是否已经登录，未登录抛异常
        StpUtil.checkLogin();
        // 当前会话注销登录
        StpUtil.logout();
        return SaResult.ok("退出登录成功");
    }


}

